<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>项目管理</title>
    <link href="/assets/images/favicon.ico" rel="icon">
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/assets/module/admin.css?v=318"/>

    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/assets/js/common.js?v=318"></script>
    <style rel="stylesheet">
        .layui-table-page {
            position: relative;
            margin-top: 0px; /* 可根据需要调整上边距 */
        }
        .layui-card-body{
            margin-top: 20px;
            padding-top: 20px;
        }
        .button-container{
            padding-bottom: 20px;
        }
        .layui-hide{
            width: 100%;
        }
        .layui-form-label
        {
            padding: 9px 8px 9px 0px;
        }



    </style>
    <script type="text/javascript">
        var url = window.location.pathname.substring(1);
        var item = url.split("/");
        var C = item[0];
        var A = item[1];
        var cUrl = "/" + C;
    </script>
</head>
<body>
<div>
    <div class="layui-card">
        <div class="layui-card-header" style="padding: 10px">
            <form class="layui-form">
                <div class="layui-form-item">
                    <!-- 项目名称 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-inline">
                            <input id="projectName" type="text" name="projectName" placeholder="请输入项目名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <!-- 开始时间 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">开始时间</label>
                        <div class="layui-input-inline">
                            <input id="beginDate" type="text" name="beginDate" placeholder="请输入开始时间" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <!-- 结束时间 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">结束时间</label>
                        <div class="layui-input-inline">
                            <input id="endDate" type="text" name="endDate" placeholder="请输入结束时间"  autocomplete="off" class="layui-input" >
                        </div>
                    </div>
                    <!-- 项目状态 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select id="state" name="state" class="layui-input">
                                <option value="">请选择状态</option>
                                <option value="0">未启动</option>
                                <option value="1">培训中</option>
                                <option value="2">培训结束</option>
                            </select>
                        </div>
                    </div>
                    <!-- 查询按钮 -->
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width: auto;">
                            <button id="search" class="layui-btn" lay-submit="" lay-filter="searchForm">
                                <i class="layui-icon layui-icon-search"></i> 查询
                            </button>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card-body">

            <div class="button-container">
                <button id="add" class="layui-btn layui-btn-primary">
                    <i class="layui-icon layui-icon-add-1"></i> 新增
                </button>
            </div>

            <table class="layui-hide" id="tableList" lay-filter="tableList"></table>
            <div class="layui-table-page"></div> <!-- 将分页栏放置在这里 -->
            <!-- 表格操作列 -->
            <script type="text/html" id="barTable">
                {{# if(d.state === 0) { }}
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                {{# } }}
            </script>

            <script th:inline="none">
                layui.use(['table', 'form', 'layer', 'laydate'], function () {
                    var $ = layui.jquery;
                    var layer = layui.layer;
                    var form = layui.form;
                    var table = layui.table;
                    var laydate = layui.laydate;

                    table.render({
                        elem: '#tableList',
                        url: '/project/list',  // 请求项目列表数据的接口
                        defaultToolbar: ['filter', 'exports', 'print'],
                        title: '项目数据',
                        cols: [[
                            {title: '序号', width: 80, templet: function (d) { return d.LAY_TABLE_INDEX + 1; }},
                            {field: 'projectName', title: '项目名称', width: 200},
                            {field: 'beginDate', title: '开始时间', width: 300, templet: function(d) {
                                    return formatDate(d.beginDate);
                                }},
                            {field: 'endDate', title: '结束时间', width: 300, templet: function(d) {
                                    return formatDate(d.endDate);
                                }},
                            {field: 'num', title: '学员人数', width: 100},
                            {field: 'state', title: '状态', width: 100, templet: function(d) {
                                    return d.state === 0 ? '未启动' : (d.state === 1 ? '培训中' : '培训结束');
                                }},
                            {fixed: 'right', title: '操作', toolbar: '#barTable', width:  300}
                        ]],
                        page: true,
                        limit: 10,
                        limits: [10,50, 100, 150, 200],
                        id: 'tableList',
                        done: function(e){
                            $('.layui-table').css("width", "100%");
                            $("th[data-field='delete']").css("border-right", 'none');
                        }
                    });

                    // 格式化日期为 yyyy-MM-dd
                    function formatDate(dateString) {
                        if (!dateString) return '';
                        var date = new Date(dateString);
                        var year = date.getFullYear(); // 获取完整年份
                        var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份
                        var day = date.getDate().toString().padStart(2, '0'); // 日
                        return `${year}-${month}-${day}`;
                    }

                    // 日期选择器 - 设置为空
                    laydate.render({
                        elem: '#beginDate',
                        type: 'date',  // 只支持日期
                        format: 'yyyy-MM-dd'  // 设置日期格式
                    });
                    laydate.render({
                        elem: '#endDate',
                        type: 'date',  // 只支持日期
                        format: 'yyyy-MM-dd'  // 设置日期格式
                    });

                    $('#search').click(function () {
                        var projectName = $('#projectName').val().trim();
                        var beginDate = $('#beginDate').val().trim();
                        var endDate = $('#endDate').val().trim();
                        var state = $('#state').val().trim();

                        // 验证参数
                        if (isNaN(state) || (state !== "" && (state < 0 || state > 2))) {
                            layer.msg('请选择有效的项目状态', {icon: 2, anim: 6});
                            return;
                        }

                        // 构建查询参数对象
                        var whereParams = {
                            projectName: projectName,
                            beginDate: beginDate,
                            endDate: endDate,
                            state: state
                        };


                        setTimeout(function () {
                            $.ajax({
                                url: '/project/list',  // 确保此 URL 与后端接口匹配
                                method: 'post',
                                contentType: 'application/json',
                                data: JSON.stringify(whereParams),  // 直接发送 JSON 数据
                                success: function (res) {
                                    layer.close(index);
                                    if (res.code === 0) {
                                        table.reload('tableList', {
                                            data: res.data,  // 假设后端返回的数据在 res.data 中
                                            page: { curr: 1 }
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 2, anim: 6});
                                    }
                                },
                                error: function (xhr, status, error) {
                                    layer.close(index);
                                    layer.msg('查询失败，请稍后重试', {icon: 2, anim: 6});
                                    console.error(xhr.responseText);  // 输出错误信息以便调试
                                }
                            });
                        }, 500);
                    });




                    // // 表格操作
                    // table.on('tool(tableList)', function (obj) {
                    //     var data = obj.data;
                    //     if (obj.event === 'del') {
                    //         layer.confirm('真的要删除该项目吗？', function (index) {
                    //             $.ajax({
                    //                 type: 'POST',
                    //                 url: '/project/del',  // 删除项目接口
                    //                 data: {id: data.id},
                    //                 success: function (res) {
                    //                     if (res.code === 0) {
                    //                         layer.msg(res.msg);
                    //                         // 访问加载列表
                    //                     } else {
                    //                         layer.msg(res.msg, {icon: 2, anim: 6});
                    //                     }
                    //                 }
                    //             });
                    //             layer.close(index);
                    //         });
                    //     } else if (obj.event === 'edit') {
                    //         layer.open({
                    //             type: 2,
                    //             title: '编辑项目',
                    //             fixed: false,
                    //             maxmin: true,
                    //             content: '/project/form?id=' + data.id,
                    //             area: ['60%', '60%']
                    //         });
                    //     }
                    // });

                    // 表格操作
                    table.on('tool(tableList)', function (obj) {
                        var data = obj.data;

                        // 编辑
                        if (obj.event === 'edit') {
                            if (data.state !== 0) {
                                layer.msg('只有未启动的项目才能编辑！', {icon: 2, anim: 6});
                            } else {
                                layer.open({
                                    type: 2,
                                    title: '编辑项目',
                                    fixed: false,
                                    maxmin: true,
                                    content: '/project/form?id=' + data.id,
                                    area: ['80%', '80%']
                                });
                            }
                        }
                        // 删除
                        else if (obj.event === 'del') {
                            if (data.state !== 0) {
                                layer.msg('只有未启动的项目才能删除！', {icon: 2, anim: 6});
                            } else {
                                layer.confirm('真的要删除该项目吗？', function (index) {
                                    $.ajax({
                                        type: 'POST',
                                        url: '/project/del',  // 删除项目接口
                                        data: {id: data.id},
                                        success: function (res) {
                                            if (res.code === 0) {
                                                layer.msg(res.msg);
                                                $('#search').click();  // 重新查询
                                            } else {
                                                layer.msg(res.msg, {icon: 2, anim: 6});
                                            }
                                        }
                                    });
                                    layer.close(index);
                                });
                            }
                        }
                    });

                    // 新增按钮点击事件
                    $('#add').click(function () {
                        layer.open({
                            type: 2,
                            title: '新增项目',
                            fixed: false,
                            maxmin: true,
                            content: '/project/form?id=0',
                            area: ['1010px', '520px']
                        });
                    });

                    // 批量导入文件上传
                    $('#batchImport').click(function () {
                        $('#excelFile').click();
                    });

                    $('#excelFile').change(function () {
                        var file = this.files[0];
                        if (!file) {
                            layer.msg('请选择文件上传');
                            return;
                        }
                        var formData = new FormData();
                        formData.append('file', file);

                        $.ajax({
                            url: '/project/batchImport',  // 批量导入接口
                            type: 'POST',
                            data: formData,
                            processData: false,
                            contentType: false,
                            success: function (res) {
                                if (res.code === 0) {
                                    layer.msg('导入成功！');
                                    $('#search').click();  // 刷新数据
                                } else {
                                    layer.msg('上传失败：' + res.msg, {icon: 2});
                                }
                            },
                            error: function () {
                                layer.msg('文件上传失败，请重试', {icon: 2});
                            }
                        });
                    });
                });

            </script>
        </div>
    </div>
</div>
<footer>
    <!-- JS部分 -->
    <script type="text/javascript">
        var url = window.location.pathname.substring(1);
        var item = url.split("/");
        var jsUrl = "/static/module/javaweb_" + item[0] + ".js";
        document.write("<script src='" + jsUrl + "'><\/script>");
    </script>
</footer>
</body>
</html>
